package indi.kider.server.appclient.api;

import indi.kider.common.model.bill.BillPay;
import indi.kider.server.appclient.model.TenantBillRequest;
import indi.kider.server.appclient.service.TenantBillService;
import indi.kider.server.appclient.service.related.BillService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.utils.page.PageRequest;

import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
@RequestMapping(value = "tenant-bill", produces = "application/json;charset=UTF-8")
public class AppClientBillController {

    //房客获取相关全部账单
    //房客根据获取相关账单详情
    //房客缴费更新账单缴费详情
    //房客删除已缴费账单
    @Autowired
    TenantBillService tenantBillService;
    @Autowired
    BillService billService;

    /**
     * @api {POST} tenant-bill/list
     * @apiGroup app-client
     * @apiDescription 房客获取相关全部账单(分页)
     * (payState : 1已支付 ， 0未支付)
     * 账单类型（1水，2电，3网，4公摊，5房租，6物业，7押金）
     * @apiParamExample 请求样例：
     * http://localhost:9002/app-client/tenant-bill/list
     * {"num":"0","size":"8","query":{"payState":1,"billType":1}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"rows":[{"id":1,"billType":1,"payState":0,"billTime":"2018-05-10 08:58:53","payTime":null,"billAttribution":1,"lastRead":"22","lastReadTime":"2018-05-10 08:59:02","thisRead":"22","thisReadTime":"2018-05-10 08:59:05","absoluteNumber":"121","price":"1","total":"1","paySerial":""}],"total":1,"size":0}}
     */
    @PostMapping("list")
    public CommonResponse getBills(@RequestBody PageRequest<TenantBillRequest> requestPageRequest, HttpServletRequest httpServletRequest) {
        String phone = httpServletRequest.getHeader("phone");
        requestPageRequest.getQuery().setPhone(phone);
        return tenantBillService.getBillsPage(requestPageRequest);
    }

    /**
     * @api {GET} tenant-bill/{billId}
     * @apiGroup app-client
     * @apiDescription 房客获取账单详情
     * @apiParamExample 请求样例：
     * http://localhost:9002/app-client/tenant-bill/1
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"id":1,"billType":1,"payState":0,"billTime":"2018-05-10 08:58:53","payTime":null,"billAttribution":1,"lastRead":"22","lastReadTime":"2018-05-10 08:59:02","thisRead":"22","thisReadTime":"2018-05-10 08:59:05","absoluteNumber":"121","price":"1","total":"1","paySerial":""}}
     */
    @GetMapping("{billId}")
    public CommonResponse getBillDetail(@PathVariable Long billId) {
        return tenantBillService.getBillDetail(billId);
    }

    /**
     * @api {POST} tenant-bill/total
     * @apiGroup app-client
     * @apiDescription 根据账单归属统计未缴账单
     * @apiParamExample 请求样例：
     * http://localhost:9002/app-client/tenant-bill/total
     * {"wxopenId":"121"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"total":"1","bills":[{"id":1,"billType":1,"payState":0,"billTime":"2018-05-10 08:58:53","payTime":null,"billAttribution":1,"lastRead":"22","lastReadTime":"2018-05-10 08:59:02","thisRead":"22","thisReadTime":"2018-05-10 08:59:05","absoluteNumber":"121","price":"1","total":"1","paySerial":""}]}}
     */
    @PostMapping("total")
    public CommonResponse getBillsTotal(@RequestBody TenantBillRequest tenantBillRequest) {
        return tenantBillService.getBillsTotal(tenantBillRequest);
    }

    /**
     * @api {PUT} tenant-bill/state
     * @apiGroup app-client
     * @apiDescription 房客缴费更新账单缴费详情
     * @apiParamExample 请求样例：
     * http://localhost:9002/app-client/tenant-bill/state
     * {"payState": 1,"payTime":"2018-05-10","paySerial":"11111","payType:"1,"billId":[1],"tenantId":1}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"202","msg":"保存成功！","data":{"id":1,"billType":null,"payState":1,"billTime":null,"payTime":"2018-05-10 08:00:00","billAttribution":null,"lastRead":null,"lastReadTime":null,"thisRead":null,"thisReadTime":null,"absoluteNumber":null,"price":null,"total":null,"paySerial":"11111"}}
     */
    @PutMapping("state")
    public CommonResponse updatePaystate(@RequestBody BillPay billPay) {
        return tenantBillService.updatePaystate(billPay);
    }

    /**
     * @api {POST} tenant-bill/state
     * @apiGroup app-client
     * @apiDescription 客户端批量删除账单显示
     * @apiParamExample 请求样例：
     * http://localhost:9002/app-client/tenant-bill/state
     * [1,2,3]
     * @apiSuccessExample {json} 返回样例:
     * {"status":"204","msg":"删除成功！","data":""}}
     */
    @PostMapping("state")
    public CommonResponse updateState(@RequestBody Long[] billIds) {
        return billService.updateState(billIds);
    }
    /**
     * @api {DELETE} tenant-bill/{billId}
     * @apiGroup app-client
     * @apiDescription 房客删除已缴费账单
     * @apiParamExample 请求样例：
     * http://localhost:9002/app-client/tenant-bill/1
     * @apiSuccessExample {json} 返回样例:
     * {"status":"204","msg":"删除成功！","data":""}
     */
    @DeleteMapping("{billId}")
    public CommonResponse delBill(@PathVariable Long billId) {
        return tenantBillService.delBill(billId);
    }
}
